<template>
  <div class="boyi-finance-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4" />
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap size="small">
            <el-form-item>
              <el-select
                v-model="queryForm.boyi_uid"
                clearable
                filterable
                placeholder="请选择操作人"
              >
                <el-option
                  v-for="(item1, index1) in userList"
                  :key="index1"
                  :label="item1.name"
                  :value="item1.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.daterange"
                type="daterange"
                date-format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryForm.to_uid"
                clearable
                filterable
                placeholder="佣金收入人"
              >
                <el-option
                  v-for="(item1, index1) in userList"
                  :key="index1"
                  :label="item1.name"
                  :value="item1.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryForm.transaction_type" clearable>
                <el-option label="收入" value="income" />
                <el-option label="支出" value="expense" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="交易类型"
        prop="transaction_type"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag v-if="row.transaction_type == 'expense'" type="danger">
            支出
          </el-tag>
          <el-tag v-else-if="row.transaction_type == 'income'" type="success">
            收入
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="类别"
        prop="category"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="金额"
        prop="amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作人"
        prop="boyi_user.name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="佣金收入人"
        prop="to_user.name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-button v-if="row.to_user" type="success" size="small">
            {{ row.to_user.name }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="关联客户"
        prop="note"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-button
            v-if="row.customer"
            type="primary"
            size="small"
            @click="customerClick(row)"
          >
            {{ row.customer.name }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="备注"
        prop="note"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="created_at"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        label="更新时间"
        prop="updated_at"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        label="状态"
        prop="note"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag v-if="row.status" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        align="center"
        label="是否发放"
        prop="note"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag v-if="row.is_pay" type="success">已发放</el-tag>
          <el-tag v-else type="danger">未发放</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button-group>
            <el-button text size="small" @click="changeRowStatus(row)">
              禁用
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <CustomerDrawer ref="customerRef" />
  </div>
</template>

<script>
  import { getList, changeStatus } from '@/api/boyiFinance'
  import Edit from './components/BoyiFinanceEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'
  import CustomerDrawer from '@/views/boyi/customer/components/CustomerDrawer'
  import { getCustomerConfig } from '@/api/config'
  export default defineComponent({
    name: 'FIndex',
    components: { Edit, CustomerDrawer },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        customerRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
        userList: [],
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const customerClick = (row) => {
        state.customerRef.showEdit(row.customer)
      }

      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }

      const changeRowStatus = async (row) => {
        console.log(row)
        if (row.status == 1) {
          $baseConfirm('你确定要禁用当前项吗', null, async () => {
            const { msg } = await changeStatus({ id: row.id, status: 0 })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          const { msg } = await changeStatus({ id: row.id, status: 1 })
          $baseMessage(msg, 'success', 'vab-hey-message-success')
          await fetchData()
        }
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const getCustomerConfigData = async () => {
        const {
          data: { userList },
        } = await getCustomerConfig()
        state.userList = userList
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
        getCustomerConfigData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        customerClick,
        Plus,
        Search,
        changeRowStatus,
      }
    },
  })
</script>
